<template>
  <!-- 配置证书模板 -->
  <div class="app-container">
    <!-- 搜索查询 -->
    <div class="form-container">
      <el-form ref="queryForm" :inline="true" label-width="80px" :model="filters">
        <el-form-item label="证件名称" prop="certificateName">
          <el-input v-model="filters.certificateName" clearable placeholder="请输入证件名称" size="small" />
        </el-form-item>
        <el-form-item label="证件状态" prop="status">
          <el-select v-model="filters.status" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <!--操作按钮-->
    <div class="btns-group">
      <el-button v-clickForbidden icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      <el-button v-clickForbidden icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <el-button icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      <el-button icon="el-icon-edit" size="mini" :disabled="editSingle" @click="handleUpdate">修改</el-button>
      <el-button icon="el-icon-zoom-in" size="mini" :disabled="single" @click="handleDetail">详情</el-button>
      <el-button icon="el-icon-delete" size="mini" :disabled="single" @click="handleStop">{{ status === '0' ? '停用' : '启用' }}</el-button>
      <span class="personal-type">
        <el-button icon="el-icon-arrow-left" size="mini" @click="handleReturn()">返回</el-button>
      </span>
    </div>
    <!-- 表格展示start -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      class="table-list"
      stripe
      :class="highLevelSet===true ? (needTagsView===true ? 'el-body-page-roll-tag-height' : 'el-body-page-roll-height') :
        (needTagsView===true ? 'el-body-page-roll-tag' : 'el-body-page-roll')"
      :data="tableData"
      :row-style="rowClass"
      @selection-change="handleSelectionChange"
      @row-click="rowChecked"
    >
      <el-table-column type="selection" width="38" />
      <el-table-column prop="certificateName" label="证件名称" min-width="180" align="center" />
      <el-table-column prop="orderNum" label="排序" min-width="120" align="center" />
      <el-table-column label="状态" min-width="120" align="center">
        <template slot-scope="{row}">
          <span v-if="row.status === '0'">启用</span>
          <span v-if="row.status === '2'">停用</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格展示end-->

    <!-- 分页器 -->
    <pagination
      class="pagination-container"
      :total="total"
      :page.sync="filters.pageNum"
      :limit.sync="filters.pageSize"
      @pagination="getList"
    />
    <!-- 证件类型模板新增修改组件  -->
    <addDialog ref="addDialog" />
    <!-- 证件类型模板详情组件  -->
    <detailDialog ref="detailDialog" />
  </div>
</template>

<script>
import { listCertificateType, removeCertificateType } from '@/api/superviseBaseInfo/typeConfig/certTypeTemplate'
import { mapState } from 'vuex'
import addDialog from './components/addDialog'
import detailDialog from './components/detailDialog'
export default {
  name: 'CertTypeTemplate',
  components: {
    addDialog,
    detailDialog
  },
  data() {
    return {
      statusOptions: [{ label: '正常', value: '0' }, { label: '停用', value: '2' }],
      status: '2',
      // 筛选条件
      highLevelSet: false, // 高级设置显示隐藏
      filters: {
        pageNum: 1,
        pageSize: 20,
        certificateName: '',
        status: '0',
        typeId: this.$route.query.id
      },
      /* 列表 start*/
      total: 0, // 分页总条数
      single: true, // 非单个禁用
      selectRow: [], // 高亮样式
      multipleSelection: [], // 选中表格行
      loading: false, // 列表加载
      tableData: [] // 表格数据
      /* 列表 end*/
    }
  },
  computed: {
    ...mapState({
      needTagsView: (state) => state.settings.tagsView
    }),
    // 高级搜索显示隐藏
    handleTagShow() {
      return this.highLevelSet
    },
    editSingle() {
      return this.multipleSelection.length === 0 || this.multipleSelection[0].status === '2'
    }
  },
  created() {
    // 获取数据
    this.getList()
  },
  methods: {
    // 返回
    handleReturn() {
      this.$router.go(-1)
    },
    handleStop() {
      const status = this.status === '0' ? '2' : '0'
      const statusText = this.status === '0' ? '停用' : '启用'
      this.$confirm(`确认要${statusText}此证件吗?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.loading = true
          const id = this.multipleSelection[0].id
          await removeCertificateType(id, status)
          if (status === '2') {
            this.msgSuccess('停用成功')
          } else {
            this.msgSuccess('启用成功')
          }
          this.loading = false
          this.getList()
        })
        .catch(() => {
          this.msgWarning('已取消!')
        })
    },
    // 获取数据
    async getList() {
      this.loading = true
      try {
        const { data: { rows, total }} = await listCertificateType(this.filters)
        this.tableData = rows
        this.total = total
        // this.addTaleData()
        this.loading = false
      } catch {
        this.loading = false
      }
    },
    // list表格填充空白行
    addTaleData() {
      if (this.tableData.length < this.fristPageSize) {
        const arr = {}
        this.tableData.push(arr)
        this.addTaleData()
      }
    },
    /* 表格选中行事件 start */
    rowChecked(row) {
      const selectData = this.multipleSelection
      this.$refs.multipleTable.clearSelection()
      if (selectData.length === 1) {
        selectData.forEach((item) => {
          // 判断 如果当前的一行被勾选, 再次点击的时候就会取消选中
          if (item === row) {
            this.$refs.multipleTable.toggleRowSelection(row, false)
          } else {
            this.$refs.multipleTable.toggleRowSelection(row, true)
          }
        })
      } else {
        this.$refs.multipleTable.toggleRowSelection(row, true)
      }
    },
    // 点击表格前面选框时触发的事件
    handleSelectionChange(val) {
      if (JSON.stringify(val[0]) === '{}') {
        this.$refs.multipleTable.clearSelection()
        val = []
      }
      if (val.length === 1) {
        this.status = val[0].status
        this.single = false
      } else {
        this.single = true
        this.status = '2'
      }
      this.selectRow = []
      this.multipleSelection = val
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item, index) => {
          this.selectRow.push(this.tableData.indexOf(item))
        })
      }
      if (val.length > 1) {
        this.$refs.multipleTable.clearSelection()
        this.$refs.multipleTable.toggleRowSelection(val.pop())
      }
    },
    // 高亮样式
    rowClass({ row, rowIndex }) {
      if (this.selectRow.includes(rowIndex)) {
        return { 'background-color': 'rgba(185,221,249,.75)' }
      }
    },
    /* 表格选中行事件 end */

    /* 按钮 Start */
    // 搜索方法
    handleQuery() {
      this.filters.pageNum = 1
      this.getList()
    },
    // 重置搜索
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 新增
    handleAdd() {
      const { id: typeId } = this.$route.query
      this.$refs.addDialog.handleAdd('新增证件信息', undefined, typeId)
    },
    // 编辑
    handleUpdate() {
      this.$refs.addDialog.handleAdd('修改证件信息', this.multipleSelection[0].id)
    },
    // 详情
    handleDetail() {
      this.$refs.detailDialog.handleAdd('证件信息详情', this.multipleSelection[0].id)
    }
    /* 按钮 end */
  }
}
</script>

<style scoped lang="scss">
</style>

